#include "StdAfx.h"
#include ".\mfcwpfcontainer.h"
#include "MFCWPFGrid.h"
#include "MFCWPFComboBox.h"
#include "MFCWPFFrame.h"
#include "MFCWPFImage.h"
#include "MFCWPFLabel.h"
#include "MFCWPFLine.h"
#include "MFCWPFTextBox.h"

CMFCWPFContainer::CMFCWPFContainer(void)
{
}

///<summary>
///Clear elements stored in m_vecElements
///</summary>
CMFCWPFContainer::~CMFCWPFContainer(void)
{
	for (vector<CMFCWPFElement *>::iterator iter = m_vecElements.begin();
		iter != m_vecElements.end();
		++iter)
	{
		CMFCWPFElement * pMFCWPFElement = *iter;
		delete pMFCWPFElement;
		*iter = NULL;
	}
}

///<summary>
///Get Node Attributes from the xaml file using the path and
///calculate the element's rect and
///parse all the child nodes belong to this element
///</summary>
///<param name='szPath'>
///the path in the xaml file
///</param>
///<param name='pXmlParser'>
///the pointer to the xml parser
///</param>
///<param name='oRect'>
///the region of the element that contains this element
///</param>
void CMFCWPFContainer::Parse(const CString & szPath, CECGXmlParser * pXamlParser, const CRect & oRect)
{
	CMFCWPFRect::Parse(szPath, pXamlParser, oRect);

	CECGXmlNode oNode;
	map<CString, int> mapTypeCount;
	int nChildCount = pXamlParser->GetChildNodeCount(szPath);
	for (int nChildIndex = 0; nChildIndex < nChildCount; ++nChildIndex)
	{
		pXamlParser->ParseChildNode(oNode, szPath, nChildIndex);
		CString szName = oNode.GetName();
		CMFCWPFElement * pMFCWPFElement = GetElementFromName(szName);
		if (pMFCWPFElement)
		{
			m_vecElements.push_back(pMFCWPFElement);
		}

		std::map<CString, int>::iterator iter = mapTypeCount.find(szName);
		if (iter != mapTypeCount.end())
		{
			iter->second++;
		}
		else
		{
			mapTypeCount.insert(pair<CString, int>(szName, 1));
			iter = mapTypeCount.find(szName);
		}
		CString szTemp;
		szTemp.Format(_T("%d"), iter->second);
		CString szChildPath = szPath + CString(_T("/")) + szName + CString(_T("[")) + szTemp + CString(_T("]")) ;
		pMFCWPFElement->Parse(szChildPath, pXamlParser, m_oRect);
	}
}

///<summary>
///Construct an element and return its pointer from the name
///</summary>
///<param name='szName'>
///the name indicates the type of the element
///</param>
///<returns>
///the pointer to the element
///</returns>
CMFCWPFElement * CMFCWPFContainer::GetElementFromName(const CString & szName)
{
	CString szNameL = szName;
	szNameL.MakeLower();
	CMFCWPFElement * pMFCWPFElement = NULL;
	if (szNameL == _ELEMENTGRID)
	{
		pMFCWPFElement = new CMFCWPFGrid;
	}
	if (szNameL == _ELEMENTCOMBOBOX)
	{
		pMFCWPFElement = new CMFCWPFComboBox;
	}
	if (szNameL == _ELEMENTFRAME)
	{
		pMFCWPFElement = new CMFCWPFFrame;
	}
	if (szNameL == _ELEMENTIMAGE)
	{
		pMFCWPFElement = new CMFCWPFImage;
	}
	if (szNameL == _ELEMENTLABEL)
	{
		pMFCWPFElement = new CMFCWPFLabel;
	}
	if (szNameL == _ELEMENTTEXTBOX)
	{
		pMFCWPFElement = new CMFCWPFTextBox;
	}
	if (szNameL == _ELEMENTLINE)
	{
		pMFCWPFElement = new CMFCWPFLine;
	}
	return pMFCWPFElement;
}

///<summary>
///Draw the element's background in the context of pCDCPlus and
///draw all the element contained in m_vecElements
///according to the "IsEnabled" attribute
///</summary>
///<param name='pCDCPlus'>
///the pointer to the GDI+ display context
///</param>
///<returns>
///true if the element is enabled; false otherwise
///</returns>
bool CMFCWPFContainer::DrawSelf(CDCPlus * pCDCPlus)
{
	if (CMFCWPFRect::DrawSelf(pCDCPlus))
	{
		for (std::vector<CMFCWPFElement *>::iterator iter = m_vecElements.begin();
			iter != m_vecElements.end();
			++iter)
		{
			(*iter)->DrawSelf(pCDCPlus);
		}
		return true;
	}
	return false;
}

///<summary>
///using the element's rect to calculate the element is clicked or not or
///calculate the element contained in m_vecElements is clicked or not
///</summary>
///<param name='nFlags'>
///show if there is other keys pushing down
///</param>
///<param name='oPoint'>
///the pointer 
///</param>
///<returns>
///the pointer to the element
///</returns>
CMFCWPFElement * CMFCWPFContainer::MouseLeftButtonUp(int nFlags, const CPoint & oPoint)
{
	CMFCWPFElement * pMFCWPFElement = CMFCWPFRect::MouseLeftButtonUp(nFlags, oPoint);
	if (pMFCWPFElement != NULL)
	{
		pMFCWPFElement = NULL;
		for (std::vector<CMFCWPFElement *>::iterator iter = m_vecElements.begin();
			iter != m_vecElements.end();
			++iter)
		{
			pMFCWPFElement = (*iter)->MouseLeftButtonUp(nFlags, oPoint);
			if (pMFCWPFElement)
			{
				break;
			}
		}
	}
	return pMFCWPFElement;
}